﻿using Qy.Core;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Qy.Scenario;

[AppService]
public class OndutyCatService : QyRepository<OndutyCat>
{
    public OndutyCatService(ApplicationContext applicationContext) : base(applicationContext)
    {
    }

    public async Task<IEnumerable<OndutyCat>> GetListAsync(QueryOndutyCat query = null)
    {
        IEnumerable<OndutyCat> dictCats = await GetAllListAsync("Id desc");
        if (query == null)
            return dictCats;
       return dictCats.Take(query.PageSize);
       //  return dictCats.Skip(query.PageNum * query.PageSize).Take(query.PageSize).ToList();
    }

    public async Task<PagedInfo<OndutyCat>> GetPageListAsync(QueryOndutyCat parm)
    {
        var exp = Expressionable.Create<OndutyCat>();
        exp = exp.AndIF(!string.IsNullOrEmpty(parm.Name), data => data.Name.Contains(parm.Name));
        return await Queryable()
            .Where(exp.ToExpression())
            .OrderBy((data) => data.Id, OrderByType.Desc)
            .ToPageAsync(parm);
    }

    public async Task<bool> DeleteDelCache(int id)
    {
        bool res = await Context.Queryable<OndutyData>().AnyAsync(data=>data.Cid==id);
        if (!res)
        {
            return await DeleteAndDelCacheAsync(id) > 0;
        }
        else return false;
    }

}
